package com.yst.suivi.data.user;

import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Collection;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;

import com.yst.suivi.data.AbstractEntityBean;

/**
 * @author stolf
 * @ejb.bean name="User" type="CMP" cmp-version="2.x" schema="user"
 *           local-jndi-name="UserBean" view-type="local" primkey-field = "id"
 *           reentrant = "True"
 * @ejb.home generate="local"
 * @ejb.persistence table-name = "usr"
 * @jboss.entity-command name = "key-generator"
 * @jboss.read-ahead eager-load-group = "most" strategy = "on-load"
 *                   page-size="25"
 * @jboss.load-group name="most"
 * @ejb.interface local-extends = "com.yst.suivi.data.util.NamedObject,
 *                javax.ejb.EJBLocalObject"
 * @ejb.finder signature="java.util.Collection findAll()" query="SELECT
 *             OBJECT(g) FROM user AS g"
 * @ejb.finder signature="java.util.Collection findAllActive(java.lang.Long
 *             endDate)" query="SELECT OBJECT(g) FROM user AS g where
 *             g.dateFinMilli>=?1 OR g.dateFinMilli=0"
 * @ejb.finder signature="com.yst.suivi.data.user.User
 *             findByLogin(java.lang.String login)" query="SELECT OBJECT(g) FROM
 *             user AS g WHERE g.login=?1"
 * @ejb.finder signature="java.util.Collection
 *             findByActivities(com.yst.suivi.data.activities.Activity act)"
 *             query="SELECT OBJECT(g) FROM user AS g, IN (g.activities) as acts
 *             WHERE acts=?1"
 */
public abstract class UserBean extends AbstractEntityBean implements EntityBean
{
	private String fullname = "".intern();

	/**
	 * @return
	 * @throws CreateException
	 * @ejb.create-method view-type = "local"
	 */
	public String ejbCreate() throws CreateException
	{
		setDateFinMilli(new Long(0));
		return null;
	}

	public void ejbPostCreate()
	{
		// by default nothing to do
	}

	/**
	 * Return the primary key of the object
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(255)"
	 */
	public abstract String getId();

	/**
	 * Fix the primary key of the object. The method should be used only by the
	 * container
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setId(String id);

	/**
	 * Return the name of the object
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.load-group name="most"
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(255)"
	 */
	public abstract String getNom();

	/**
	 * Fix the name of the user. The method should be used only by the container
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setNom(String nom);

	/**
	 * Return the name of the object
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.load-group name="most"
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(255)"
	 */
	public abstract String getPrenom();

	/**
	 * Fix the name of the user. The method should be used only by the container
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setPrenom(String prenom);

	/**
	 * Return the name of the object
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence column-name = "PrincipalID"
	 * @jboss.load-group name="most"
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(255)"
	 */
	public abstract String getLogin();

	/**
	 * Fix the name of the user. The method should be used only by the container
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setLogin(String login);

	/**
	 * Return the name of the object
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.load-group name="most"
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(255)"
	 */
	public abstract String getPwd();

	/**
	 * Fix the name of the user. The method should be used only by the container
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setPwd(String nom);

	/**
	 * Retourne le nom complet de l'utilisateur
	 * 
	 * @return
	 * @ejb.interface-method view-type = "local"
	 */
	public String getFullname()
	{
		return fullname;
	}

	/**
	 * Return the firm of the object
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.load-group name="most"
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(255)"
	 */
	public abstract String getSociete();

	/**
	 * Fix the firm of the user.
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setSociete(String nom);

	/**
	 * Return the mail of the user
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.load-group name="most"
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(255)"
	 */
	public abstract String getMail();

	/**
	 * Fix the mail of the user.
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setMail(String mail);

	/**
	 * Return the code used to identify the person.
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.load-group name="most"
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(255)"
	 */
	public abstract String getCode();

	/**
	 * Fix the code used to identify the person.
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setCode(String nom);

	/**
	 * Return the code used to identify the person.
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.load-group name="most"
	 */
	public abstract Float getTjm();

	/**
	 * Fix the code used to identify the person.
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setTjm(Float tjm);

	/**
	 * @ejb.relation name="activity-user" role-name = "User-activityTargetRole"
	 *               target-multiple = "yes"
	 * @jboss.relation-mapping style = "relation-table"
	 * @jboss.relation-table table-name = "LnkActivityUser"
	 * @jboss.relation fk-column = "idActivity" related-pk-field = "id"
	 * @ejb.interface-method view-type = "local"
	 * @return
	 */
	public abstract Collection getActivities();

	/**
	 * @ejb.interface-method view-type = "local"
	 * @param activities
	 */
	public abstract void setActivities(Collection activities);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.suivi.data.AbstractEntityBean#ejbLoad()
	 */
	public void ejbLoad() throws EJBException, RemoteException
	{
		super.ejbLoad();
		StringBuffer buf = new StringBuffer();
		buf.append(getPrenom());
		buf.append(" ");
		buf.append(getNom());
		fullname = buf.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj)
	{
		User usr = (User) obj;
		return usr.getId().equals(getId());
	}

	/**
	 * Fix the end date in milli second
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setDateFinMilli(Long milli);

	/**
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.load-group name="most"
	 * @jboss.jdbc-type type = "BIGINT"
	 * @jboss.sql-type type = "BIGINT"
	 * @return
	 */
	public abstract Long getDateFinMilli();

	/**
	 * @ejb.interface-method view-type = "local"
	 * @return
	 */
	public Calendar getEndDate()
	{
		Calendar retour = null;
		if (0 != getDateFinMilli().longValue())
		{
			retour = Calendar.getInstance();
			retour.setTimeInMillis(getDateFinMilli().longValue());
		}
		return retour;
	}

	/**
	 * @ejb.interface-method view-type = "local"
	 */
	public void setEndDate(Calendar cal)
	{
		if (null != cal)
		{
			setDateFinMilli(new Long(cal.getTimeInMillis()));
		}
		else
		{
			setDateFinMilli(new Long(0));
		}
	}
}
